package br.com.toscano.contpedido.view;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;

import br.com.toscano.contpedido.business.ClienteBC;
import br.com.toscano.contpedido.otd.ClienteOTD;
import br.com.toscano.contpedido.util.Util;
import br.gov.frameworkdemoiselle.annotation.NextView;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractListPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;


@ViewController
@NextView("./cliente_extrato_relatorio.jsf")
@PreviousView("./cliente_extrato_relatorio.jsf")
public class ClienteExtratoRelatorioMB extends AbstractListPageBean<ClienteOTD, Long> {

	private static final long serialVersionUID = 1L;
	
	private Date dataInicial;
	private Date dataFinal;
	
	@Inject
	private ClienteBC clienteBC;

	private boolean filtrar = false;
	public void filtrar() {
		super.clear();
		filtrar = true;
	}
	
	@Override
	protected List<ClienteOTD> handleResultList() {

		List<ClienteOTD> lista = null;
		if (filtrar) {
			lista = clienteBC.gerarExtratoPorPeriodo(dataInicial, dataFinal);
		} else {
			lista = new ArrayList<ClienteOTD>();
		}
			

		return lista;
	}
	
	@Transactional
	public String deleteSelection() {
		boolean delete;
		for (Iterator<Long> iter = getSelection().keySet().iterator(); iter.hasNext();) {
			Long id = iter.next();
			delete = getSelection().get(id);
			if (delete) {
//				pedidoBC.registrarCancelamento(id);
//				iter.remove();
			}
		}
		return getPreviousView();
	}
	
	public void carregarPedido() {
		
	}

	public Date getDataInicial() {
		return dataInicial;
	}

	public void setDataInicial(Date dataInicial) {
		this.dataInicial = dataInicial;
	}

	public Date getDataFinal() {
		return dataFinal;
	}

	public void setDataFinal(Date dataFinal) {
		this.dataFinal = dataFinal;
	}
	

	public void postProcessXLS(Object document) {


		HSSFWorkbook wb = (HSSFWorkbook) document;
		HSSFSheet sheet = wb.getSheetAt(0);
		
		/******************** HEADER **********************/		
		CellStyle headerStyle = wb.createCellStyle();
		Font headerFont = wb.createFont();
		headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		headerFont.setColor(HSSFColor.BLUE.index);
		headerStyle.setFont(headerFont);
		headerStyle.setAlignment(CellStyle.ALIGN_CENTER);
		 
		Row headerRow = sheet.getRow(0);
		int cont = 0; 
		for (Cell cell : headerRow) {
			cell.setCellStyle(headerStyle);
			if (0 == cont) {
				cell.setCellValue("Nome");
			} else if (cont ==1) {
				cell.setCellValue("Saldo Anterior");
			} else if (cont ==2) {
				cell.setCellValue("Valor dos Pedidos");
			} else if (cont ==3) {
				cell.setCellValue("Valor dos Pagamentos");
			} else if (cont ==4) {
				cell.setCellValue("Saldo no Período");
			} else if (cont ==4) {
				cell.setCellValue("Valor Total Extrato");
			}
			cont++;
		}
		
		/******************** TOTAL **********************/
		CellStyle style = wb.createCellStyle();
		Font font = wb.createFont();
		font.setBoldweight(Font.BOLDWEIGHT_BOLD);
		style.setFont(font);
		style.setAlignment(CellStyle.ALIGN_RIGHT);
		Row totalRow = sheet.createRow(sheet.getLastRowNum()+2);
		
		BigDecimal saldoAnterior = BigDecimal.ZERO;
		BigDecimal valorPedidos = BigDecimal.ZERO;
		BigDecimal valorPagamentos = BigDecimal.ZERO;
		BigDecimal saldoPeriodo = BigDecimal.ZERO;
		BigDecimal extratoClientePeriodo = BigDecimal.ZERO;
		List<ClienteOTD> clientes = this.getResultList();
		for (ClienteOTD clienteOtd: clientes) {
			saldoAnterior = saldoAnterior.add(clienteOtd.getSaldoAnterior());
			valorPedidos = valorPedidos.add(clienteOtd.getValorPedido());
			valorPagamentos = valorPagamentos.add(clienteOtd.getValorPagamento());
			saldoPeriodo = saldoPeriodo.add(clienteOtd.getSaldoPeriodo());
			extratoClientePeriodo = extratoClientePeriodo.add(clienteOtd.getSaldoExtrato());
		}
		Cell cell1 = totalRow.createCell(1);
		cell1.setCellValue("R$ " + Util.formataValorParaString(saldoAnterior, 2));
		cell1.setCellStyle(style);

		cell1 = totalRow.createCell(2);
		cell1.setCellValue("R$ " + Util.formataValorParaString(valorPedidos, 2));
		cell1.setCellStyle(style);

		cell1 = totalRow.createCell(3);
		cell1.setCellValue("R$ " + Util.formataValorParaString(valorPagamentos, 2));
		cell1.setCellStyle(style);

		cell1 = totalRow.createCell(4);
		cell1.setCellValue("R$ " + Util.formataValorParaString(saldoPeriodo, 2));
		cell1.setCellStyle(style);

		cell1 = totalRow.createCell(5);
		cell1.setCellValue("R$ " + Util.formataValorParaString(extratoClientePeriodo, 2));
		cell1.setCellStyle(style);

		//Ajuste do tamanho das colunas
		sheet.autoSizeColumn(0);
		sheet.autoSizeColumn(1);
		sheet.autoSizeColumn(2);
		sheet.autoSizeColumn(3);
		sheet.autoSizeColumn(4);
		sheet.autoSizeColumn(5);
	}
}